<?php
namespace app\common\model;
use think\Model;
use think\Db;
class Admin extends Model
{
    //定义表名
    protected $name = 'shequshop_school_admin';
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){
        $data['create_time'] = time();
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){
        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():[];
    }
    /**
     * @author chenniang
     * @DataTime: 2022-06-08 17:21
     * @功能说明:加盟商列表
     */
    public function adminUserList($dis,$page){
        $data = $this->alias('a')
                ->join('massage_service_user_list b','a.user_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.nickName')
                ->group('a.id')
                ->order('a.id')
                ->paginate($page)
                ->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2022-06-08 23:06
     * @功能说明:加盟商
     */
    function jionAdminCheck($input){
        if(!empty($input['username']&&$input['username']=='admin')){
            return ['code'=>500,'msg'=>'用户名不能和超管账号相同'];
        }
        $dis[] = ['user_id','=',$input['user_id']];
        $dis[] = ['is_admin','=',0];
        $dis[] = ['uniacid','=',$input['uniacid']];
        $dis[] = ['status','>',-1];
        if(!empty($input['id'])){
            $dis[] = ['id','<>',$input['id']];
        }
        $find = $this->where($dis)->find();
        if(!empty($find)){
            return ['code'=>500,'msg'=>'该用户已经绑定过加盟商'];
        }
        $where[] = ['username','=',$input['username']];
        $where[] = ['uniacid','=',$input['uniacid']];
        $where[] = ['status','>',-1];
        if(!empty($input['id'])){
            $where[] = ['id','<>',$input['id']];
        }
        $find = $this->where($where)->find();
        if(!empty($find)){
            return ['code'=>500,'msg'=>'已经有该用户名的账户'];
        }
        return true;
    }
}